//
//  YYMainViewController.swift
//  YYWeibo
//
//  Created by strongwallyi on 15/12/26.
//  Copyright © 2015年 strongwallyi. All rights reserved.
//

import UIKit

class YYMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let tabbar = YYTabBar()
        
        tabbar.composeButtonClickClourse = {[weak self] in
            if self != nil {
                let composeView = YYComposeView()
                composeView.show(self!)
            }
        }
        setValue(tabbar, forKeyPath: "tabBar")
        
        addChildViewController()
    }

    //添加控制器
    func addChildViewController() {
        addChildViewController(YYHomeTableViewController(), imageName: "tabbar_home", title: "首页")
        
        addChildViewController(YYMessageTableViewController(), imageName: "tabbar_message_center", title: "消息")
        
        addChildViewController(YYDiscoverTableViewController(), imageName: "tabbar_discover", title: "发现")
        
        addChildViewController(YYProfileTableViewController(), imageName: "tabbar_profile", title: "我")
    }
    
    //添加四个子控制器
    func addChildViewController(vc: UIViewController, imageName: String, title: String) {
        
        
        vc.tabBarItem = YYTabBarItem()
        //将控制器的 tabbaritem 改成自定义的 item
//        vc.tabBarItem = YYTabBarItem()
        //设置文字
        vc.title = title
        //设置 item 的图片
        vc.tabBarItem.image = UIImage(named: imageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        //item 选中时的图片
        vc.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        //设置选中颜色
        let dict = [
            NSForegroundColorAttributeName: UIColor.orangeColor()
        ]
        vc.tabBarItem.setTitleTextAttributes(dict, forState: UIControlState.Selected)
        
        //添加子控制器
        addChildViewController(YYNavigationController(rootViewController: vc))
        
    }
}
